#!/bin/bash
#SBATCH --spread-job
# Copyright (c) 2020 Presto Labs Pte. Ltd.

if [ -z ${SLURM_JOB_ID} ]; then
  (>&2 echo "ERROR: run this script on slurm.")
  exit 1
fi

set -e 
export PYTHONUNBUFFERED=1
REMOTE_HOME={{remote_home}}
source ${REMOTE_HOME}/miniconda3/etc/profile.d/conda.sh
conda activate ${REMOTE_HOME}/{{coin_env}}

NUM_PARALLEL="$((${SLURM_NTASKS} + 30))"
parallel -j "${NUM_PARALLEL}" --delay 0.1 <<EOF
{% for strat_file in strat_file_list %}
srun -N1 -n1 -l --exclusive --unbuffered \
  ./pyrunner coin/tool/strat_monitor/stream_processor_2.py \
    --kafka_config_filename={{kafka_config_filename}} \
    --strategy_config_filename={{strat_file}} \
    {% if offset_hour is not none -%}
      --offset_hour={{offset_hour}} \
    {%- else -%}
      \
    {%- endif %}
    {% if pnl_adj_file is not none -%}
      --pnl_adj_file={{pnl_adj_file}} \
    {%- else -%}
      \
    {%- endif %}
    {% if realtime_pnl_adj == true -%}
      --realtime_pnl_adj \
    {%- else -%}
      \
    {%- endif %}
    --dump_result_after_sec={{dump_result_after_sec}} \
    --dump_result_frequency={{dump_result_frequency}} \
    --exit_after_min={{exit_after_min}} \
    {% if memcached_config is not none -%}
      --memcached_config={{memcached_config}} \
    {%- else -%}
      \
    {%- endif %}
    {% if plot_pnl == true -%}
      --plot_pnl \
    {%- else -%}
      \
    {%- endif %}
    {% if plot_order == true -%}
      --plot_order \
    {%- else -%}
      \
    {%- endif %}
    {% if plot_dir is not none -%}
      --plot_dir={{plot_dir}} \
    {%- else -%}
      \
    {%- endif %}
{% endfor %}
EOF
